package me.liuhui.mall.manager.web.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.core.math.Calculator;
import me.liuhui.mall.common.base.enums.VoCodeEnum;
import me.liuhui.mall.common.base.vo.ResultVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.awt.*;

import static me.liuhui.mall.manager.service.security.KaptchaAuthenticationProvider.CAPTCHA_NAME;

/**
 * Created on 2020/10/23 11:05
 * <p>
 * Description: [TODO]
 * <p>
 *
 * @author [清远]
 */

@RequestMapping("/api/auth")
@RestController
public class AuthController {


    @GetMapping("/captcha")
    public ResultVO<String> captcha(HttpSession session) {
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(97, 38);
        // 自定义验证码内容为四则运算方式
        captcha.setGenerator(new MathGenerator(1));
        // 重新生成code
        captcha.setFont(new Font("Fixedsys", Font.BOLD, 32));
        captcha.createCode();
//         获取运算的结果
        String code = String.valueOf((int) Calculator.conversion(captcha.getCode()));
        String img = captcha.getImageBase64Data();
        session.setAttribute(CAPTCHA_NAME, code);
        return new ResultVO<>(VoCodeEnum.SUCCESS, img);
    }


}
